package off_trial

import (
	// "bp/cache"
	"bp/conf"
	"bp/db"

	// "bgs/grpc/client"
	internalHttpMiddleware "bp/internal/middleware/http"
	"bp/internal/service/com"
	"bp/internal/service/entity"
	"bp/internal/twirpex"
	"go.uber.org/zap"

	// "bp/internal/util"
	offTrialGrpc "bp/rpc/gen/mwebapi/off_trial"
	messageGrpc "bp/rpc/gen/shared/message"
	"context"

	"github.com/go-chi/chi"
	"github.com/golang/protobuf/ptypes/empty"
)

type OffTrialMWebApiImp struct {
	dao       *db.PgDao
	slog      *zap.SugaredLogger
	vars      *conf.VarsSection
	entityHub *entity.EntityHub
	comHub    *com.ComHub
	// cacher *cache.Adapter
	offTrialGrpc.OffTrialOpServiceServer
}

// NewOffTrialMWebApi initialization
func NewOffTrialMWebApi(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
	entityHub *entity.EntityHub,
	comHub *com.ComHub,
	// adp *cache.Adapter,
) (ws *OffTrialMWebApiImp) {
	ws = &OffTrialMWebApiImp{
		dao:       pgdao,
		slog:      slog.With("module", "OffTrialMWebApi"),
		vars:      vars,
		entityHub: entityHub,
		comHub:    comHub,
		// cacher:           adp,
	}
	return
}

const (
	EntityName            string = "优惠试算"
	PingMethod            string = "/off_trial/ping/exec" // GET
	DoTrialMethod         string = "/off_trial"           // POST
	GetOffTrialInfoMethod string = "/off_trial"           // GET
)

// RegisterToWeb 将部分grpc方法注册到web
func (s *OffTrialMWebApiImp) RegisterToWeb(r chi.Router) {
	offTrialRT := twirpex.NewSingleTwirpServerProxy(offTrialGrpc.NewOffTrialOpServiceServer(s))

	r.Group(func(r chi.Router) {
		r.Get(PingMethod, offTrialRT.Handle("PingOffTrial"))
		r.Group(func(r chi.Router) {
			r.Use(internalHttpMiddleware.WithAuthorizationToken(s.slog, s.vars))
			r.Post(DoTrialMethod, offTrialRT.Handle("DoTrial"))
			r.Get(GetOffTrialInfoMethod, offTrialRT.Handle("GetOffTrialInfo"))
		})
	})
}

func (s *OffTrialMWebApiImp) PingOffTrial(ctx context.Context, _ *empty.Empty) (res *messageGrpc.PingRes, err error) {
	res = &messageGrpc.PingRes{
		Msg: "ok",
	}
	return
}
